﻿using System;
using System.Collections.Generic;
using ECare.BUS;
using ECare.BUS.Data;
using ECare.Client.templates;

namespace ECare.Client.frms
{
    public partial class frmSupplyQuery : frmInfo
    {
        public frmSupplyQuery()
        {
            InitializeComponent();

            Load += frmSupplyQuery_Load;
        }

        private void frmSupplyQuery_Load(object sender, EventArgs e)
        {
            //throw new NotImplementedException();

            dateStart.Value = DateTime.Now.AddMonths(-1);
            dateEnd.Value = DateTime.Now;


            //    this.cbResources.DataSource = ResourcesHelper.GetAllResources();
            //     this.cbResources.DisplayMember = "ResourceName";
            //    this.cbResources.ValueMember = "ResourceId";

            List<ResoucesInfo> resources = ResourcesHelper.GetAllResources();
            foreach (ResoucesInfo resource in resources)
            {
                cbResources.Items.Add(resource);
            }

            cbResources.Items.Insert(0, new ResoucesInfo {ResourceId = -1, ResourceName = "-请选择(please select)-"});
            cbResources.SelectedIndex = 0;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string searchwhere = "1=1";

            if (cbIsEnabled.Checked)
            {
                if (dateEnd.Value < dateStart.Value)
                {
                    errorProvider1.SetError(dateEnd, "invalid");
                    return;
                }
                errorProvider1.SetError(dateEnd, string.Empty);
                searchwhere = searchwhere + string.Format("  and supply_date between '{0}'   and '{1}'"
                    , dateStart.Value.ToShortDateString()
                    , dateEnd.Value.AddDays(1).ToShortDateString()
                    );
            }
            if (cbResources.SelectedIndex != 0)
            {
                searchwhere = searchwhere +
                              string.Format("   and   resource_Id={0}",
                                  (cbResources.SelectedItem as ResoucesInfo).ResourceId);
            }

            string orwhere = "1<>1";

            if (cbUnchecked.Checked)
            {
                orwhere = orwhere + "   or  supply_checkedstatus=0 ";
            }
            if (cbAccecpted.Checked)
            {
                orwhere = orwhere + "  or supply_checkedstatus=" + (int) CheckStatus.Qualitified;
            }

            if (cbRejected.Checked)
            {
                orwhere = orwhere + " or supply_checkedstatus=" + (int) CheckStatus.Rejected;
            }

            if (cbunFinished.Checked)
            {
                orwhere = orwhere + " or supply_checkedstatus=" + (int) CheckStatus.UnFinished;
            }

            if (cbUnchecked.Checked || cbAccecpted.Checked
                || cbunFinished.Checked || cbRejected.Checked
                )
            {
                searchwhere = searchwhere + string.Format("   and   ({0})", orwhere);
            }


            if (SearchCompleted != null)
            {
                SearchCompleted(searchwhere);
            }
            Close();
        }


        public event SeachCompletedhanlder SearchCompleted;

        private void cbIsEnabled_CheckedChanged(object sender, EventArgs e)
        {
            dateEnd.Enabled = cbIsEnabled.Checked;
            dateStart.Enabled = cbIsEnabled.Checked;
        }
    }


    public delegate void SeachCompletedhanlder(string where);
}